#ifndef __OCPP_Cesium_Rectangle_H__
#define __OCPP_Cesium_Rectangle_H__

#include "Ellipsoid.h"
#include "Cartographic.h"

namespace OCPP
{
	namespace Cesium
	{
#define MAX_VALUE_D std::numeric_limits<double>::infinity()
		class _CesiumExport Rectangle
		{
		public:
			double west, south, east, north;
			Rectangle(double _w = 0.0, double _s = 0.0, double _e = 0.0, double _n = 0.0)
				: west(_w), south(_s), east(_e), north(_n)
			{
			}

			Rectangle(const Rectangle &other)
			{
				this->west = other.west;
				this->south = other.south;
				this->east = other.east;
				this->north = other.north;
			}

			Rectangle &operator=(const Rectangle &other)
			{
				this->west = other.west;
				this->south = other.south;
				this->east = other.east;
				this->north = other.north;

				return (Rectangle &)*this;
			}

			double getWidth()
			{
				return Rectangle::computeWidth(*this);
			}

			double getHeight()
			{
				return Rectangle::computeHeight(*this);
			}

			/**
			 * The number of elements used to pack the object into an array1.
			 * @type {number}
			 */
			inline static const int packedLength = 4;

			/**
			 * Stores the provided instance into the provided array1.
			 *
			 * @param {Rectangle} value The value to pack.
			 * @param {number[]} array1 The array1 to pack into.
			 * @param {number} [startingIndex=0] The index into the array1 at which to start packing the elements.
			 *
			 * @returns {number[]} The array1 that was packed into
			 */
			static DoubleVector pack(Rectangle &value, DoubleVector array1, size_t startingIndex = 0)
			{
				array1[startingIndex++] = value.west;
				array1[startingIndex++] = value.south;
				array1[startingIndex++] = value.east;
				array1[startingIndex] = value.north;

				return array1;
			};

			/**
			 * Retrieves an instance from a packed array1.
			 *
			 * @param {number[]} array1 The packed array1.
			 * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
			 * @param {Rectangle} [result] The object into which to store the result.
			 * @returns {Rectangle} The modified result parameter or a Rectangle instance if one was not provided.
			 */
			static Rectangle unpack(DoubleVector array1, size_t startingIndex = 0)
			{
				Rectangle result;
				result.west = array1[startingIndex++];
				result.south = array1[startingIndex++];
				result.east = array1[startingIndex++];
				result.north = array1[startingIndex];
				return result;
			};

			/**
			 * Flattens an array1 of Cartesian2s into an array1 of components.
			 *
			 * @param {Rectangle[]} array1 The array1 of cartesians to pack.
			 * @param {number[]} [result] The array1 onto which to store the result. If this is a typed array1, it must have array1.length * 3 components, else a {@link DeveloperError} will be thrown. If it is a regular array1, it will be resized to have (array1.length * 3) elements.
			 * @returns {number[]} The packed array1.
			 */
			static DoubleVector packArray(std::vector<Rectangle> array1)
			{
				size_t length = array1.size();
				size_t resultLength = length * 2;
				DoubleVector result;
				result.resize(resultLength);

				for (size_t i = 0; i < length; ++i)
				{
					Rectangle::pack(array1[i], result, i * 2);
				}
				return result;
			};

			/**
			 * Unpacks an array1 of cartesian components into an array1 of Cartesian2s.
			 *
			 * @param {number[]} array1 The array1 of components to unpack.
			 * @param {Rectangle[]} [result] The array1 onto which to store the result.
			 * @returns {Rectangle[]} The unpacked array1.
			 */
			static std::vector<Rectangle> unpackArray(DoubleVector array1)
			{
				size_t length = array1.size();
				std::vector<Rectangle> result;
				result.resize(length / 2);
				for (size_t i = 0; i < length; i += 2)
				{
					size_t index = i / 2;
					result[index] = (Rectangle::unpack(array1, i));
				}
				return result;
			};

			/**
			 * Computes the width of a rectangle in radians.
			 * @param {Rectangle} rectangle The rectangle to compute the width of.
			 * @returns {number} The width.
			 */
			static double computeWidth(Rectangle rectangle)
			{
				auto east = rectangle.east;
				auto west = rectangle.west;
				if (east < west)
				{
					east += CesiumMath::TWO_PI;
				}
				return east - west;
			};

			/**
			 * Computes the height of a rectangle in radians.
			 * @param {Rectangle} rectangle The rectangle to compute the height of.
			 * @returns {number} The height.
			 */
			static double computeHeight(Rectangle rectangle)
			{
				return rectangle.north - rectangle.south;
			};

			/**
			 * Creates a rectangle given the boundary longitude and latitude in degrees.
			 *
			 * @param {number} [west=0.0] The westernmost longitude in degrees in the range [-180.0, 180.0].
			 * @param {number} [south=0.0] The southernmost latitude in degrees in the range [-90.0, 90.0].
			 * @param {number} [east=0.0] The easternmost longitude in degrees in the range [-180.0, 180.0].
			 * @param {number} [north=0.0] The northernmost latitude in degrees in the range [-90.0, 90.0].
			 * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
			 * @returns {Rectangle} The modified result parameter or a Rectangle instance if none was provided.
			 *
			 * @example
			 * const rectangle = Cesium.Rectangle.fromDegrees(0.0, 20.0, 10.0, 30.0);
			 */
			static Rectangle fromDegrees(
				double west = 0.0, double south = 0.0,
				double east = 0.0, double north = 0.0)
			{

				Rectangle result;
				result.west = CesiumMath::toRadians(west);
				result.south = CesiumMath::toRadians(south);
				result.east = CesiumMath::toRadians(east);
				result.north = CesiumMath::toRadians(north);

				return result;
			};

			/**
			 * Creates a rectangle given the boundary longitude and latitude in radians.
			 *
			 * @param {number} [west=0.0] The westernmost longitude in radians in the range [-Math.PI, Math.PI].
			 * @param {number} [south=0.0] The southernmost latitude in radians in the range [-Math.PI/2, Math.PI/2].
			 * @param {number} [east=0.0] The easternmost longitude in radians in the range [-Math.PI, Math.PI].
			 * @param {number} [north=0.0] The northernmost latitude in radians in the range [-Math.PI/2, Math.PI/2].
			 * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
			 * @returns {Rectangle} The modified result parameter or a Rectangle instance if none was provided.
			 *
			 * @example
			 * const rectangle = Cesium.Rectangle.fromRadians(0.0, Math.PI/4, Math.PI/8, 3*Math.PI/4);
			 */
			static Rectangle fromRadians(
				double west = 0.0, double south = 0.0,
				double east = 0.0, double north = 0.0)
			{
				Rectangle result;

				result.west = west;
				result.south = south;
				result.east = east;
				result.north = north;

				return result;
			};

			/**
			 * Creates the smallest possible Rectangle that encloses all positions in the provided array.
			 *
			 * @param {Cartographic[]} cartographics The list of Cartographic instances.
			 * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
			 * @returns {Rectangle} The modified result parameter or a Rectangle instance if none was provided.
			 */
			static Rectangle fromCartographicArray(std::vector<Cartographic> cartographics)
			{
				Rectangle result;

				auto west = MAX_VALUE_D;
				auto east = -MAX_VALUE_D;
				auto westOverIDL = MAX_VALUE_D;
				auto eastOverIDL = -MAX_VALUE_D;
				auto south = MAX_VALUE_D;
				auto north = -MAX_VALUE_D;

				for (auto i = 0; i < cartographics.size(); i++)
				{
					auto position = cartographics[i];
					west = (std::min)(west, position.longitude);
					east = (std::max)(east, position.longitude);
					south = (std::min)(south, position.latitude);
					north = (std::max)(north, position.latitude);

					auto lonAdjusted =
						position.longitude >= 0
							? position.longitude
							: position.longitude + CesiumMath::TWO_PI;
					westOverIDL = (std::min)(westOverIDL, lonAdjusted);
					eastOverIDL = (std::max)(eastOverIDL, lonAdjusted);
				}

				if (east - west > eastOverIDL - westOverIDL)
				{
					west = westOverIDL;
					east = eastOverIDL;

					if (east > CesiumMath::PI)
					{
						east = east - CesiumMath::TWO_PI;
					}
					if (west > CesiumMath::PI)
					{
						west = west - CesiumMath::TWO_PI;
					}
				}

				result.west = west;
				result.south = south;
				result.east = east;
				result.north = north;
				return result;
			};

			/**
			 * Creates the smallest possible Rectangle that encloses all positions in the provided array.
			 *
			 * @param {Cartesian3[]} cartesians The list of Cartesian instances.
			 * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid the cartesians are on.
			 * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
			 * @returns {Rectangle} The modified result parameter or a Rectangle instance if none was provided.
			 */
			static Rectangle fromCartesianArray(std::vector<Cartesian3> cartesians,
												Ellipsoid ellipsoid = Ellipsoid::WGS84)
			{

				Rectangle result;

				auto west = MAX_VALUE_D;
				auto east = -MAX_VALUE_D;
				auto westOverIDL = MAX_VALUE_D;
				auto eastOverIDL = -MAX_VALUE_D;
				auto south = MAX_VALUE_D;
				auto north = -MAX_VALUE_D;

				for (auto i = 0; i < cartesians.size(); i++)
				{
					Cartographic position = ellipsoid.cartesianToCartographic(cartesians[i]);
					west = (std::min)(west, position.longitude);
					east = (std::max)(east, position.longitude);
					south = (std::min)(south, position.latitude);
					north = (std::max)(north, position.latitude);

					double lonAdjusted =
						position.longitude >= 0
							? position.longitude
							: position.longitude + CesiumMath::TWO_PI;
					westOverIDL = (std::min)(westOverIDL, lonAdjusted);
					eastOverIDL = (std::max)(eastOverIDL, lonAdjusted);
				}

				if (east - west > eastOverIDL - westOverIDL)
				{
					west = westOverIDL;
					east = eastOverIDL;

					if (east > CesiumMath::PI)
					{
						east = east - CesiumMath::TWO_PI;
					}
					if (west > CesiumMath::PI)
					{
						west = west - CesiumMath::TWO_PI;
					}
				}

				result.west = west;
				result.south = south;
				result.east = east;
				result.north = north;
				return result;
			};

			static bool equals(Rectangle left, Rectangle right)
			{
				return (
					left.west == right.west &&
					left.south == right.south &&
					left.east == right.east &&
					left.north == right.north);
			};

			/**
			 * Checks a Rectangle's properties and throws if they are not in valid ranges.
			 *
			 * @param {Rectangle} rectangle The rectangle to validate
			 *
			 * @exception {DeveloperError} <code>north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
			 * @exception {DeveloperError} <code>south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
			 * @exception {DeveloperError} <code>east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
			 * @exception {DeveloperError} <code>west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
			 */
			static bool validate(Rectangle rectangle)
			{
				auto west = rectangle.west;
				auto south = rectangle.south;
				auto east = rectangle.east;
				auto north = rectangle.north;

				if (north < -CesiumMath::PI_OVER_TWO || north > CesiumMath::PI_OVER_TWO)
				{
					return false;
				}

				if (south < -CesiumMath::PI_OVER_TWO || south > CesiumMath::PI_OVER_TWO)
				{
					return false;
				}

				if (west < -CesiumMath::PI || west > CesiumMath::PI)
				{
					return false;
				}

				if (east < -CesiumMath::PI || east > CesiumMath::PI)
				{
					return false;
				}

				return true;
			};

			/**
			 * Computes the southwest corner of a rectangle.
			 *
			 * @param {Rectangle} rectangle The rectangle for which to find the corner
			 * @param {Cartographic} [result] The object onto which to store the result.
			 * @returns {Cartographic} The modified result parameter or a Cartographic instance if none was provided.
			 */
			static Cartographic southwest(Rectangle rectangle)
			{
				return Cartographic(rectangle.west, rectangle.south);
			};

			/**
			 * Computes the northwest corner of a rectangle.
			 *
			 * @param {Rectangle} rectangle The rectangle for which to find the corner
			 * @param {Cartographic} [result] The object onto which to store the result.
			 * @returns {Cartographic} The modified result parameter or a Cartographic instance if none was provided.
			 */
			static Cartographic northwest(Rectangle rectangle)
			{
				return Cartographic(rectangle.west, rectangle.north);
			};

			/**
			 * Computes the northeast corner of a rectangle.
			 *
			 * @param {Rectangle} rectangle The rectangle for which to find the corner
			 * @param {Cartographic} [result] The object onto which to store the result.
			 * @returns {Cartographic} The modified result parameter or a Cartographic instance if none was provided.
			 */
			static Cartographic northeast(Rectangle rectangle)
			{
				return Cartographic(rectangle.east, rectangle.north);
			};

			/**
			 * Computes the southeast corner of a rectangle.
			 *
			 * @param {Rectangle} rectangle The rectangle for which to find the corner
			 * @param {Cartographic} [result] The object onto which to store the result.
			 * @returns {Cartographic} The modified result parameter or a Cartographic instance if none was provided.
			 */
			static Cartographic southeast(Rectangle rectangle)
			{
				return Cartographic(rectangle.east, rectangle.south);
			};

			/**
			 * Computes the center of a rectangle.
			 *
			 * @param {Rectangle} rectangle The rectangle for which to find the center
			 * @param {Cartographic} [result] The object onto which to store the result.
			 * @returns {Cartographic} The modified result parameter or a Cartographic instance if none was provided.
			 */
			static Cartographic center(Rectangle rectangle)
			{
				auto east = rectangle.east;
				auto west = rectangle.west;

				if (east < west)
				{
					east += CesiumMath::TWO_PI;
				}

				auto longitude = CesiumMath::negativePiToPi((west + east) * 0.5);
				auto latitude = (rectangle.south + rectangle.north) * 0.5;

				return Cartographic(longitude, latitude);
			};

			/**
			 * Computes the intersection of two rectangles.  This function assumes that the rectangle's coordinates are
			 * latitude and longitude in radians and produces a correct intersection, taking into account the fact that
			 * the same angle can be represented with multiple values as well as the wrapping of longitude at the
			 * anti-meridian.  For a simple intersection that ignores these factors and can be used with projected
			 * coordinates, see {@link Rectangle.simpleIntersection}.
			 *
			 * @param {Rectangle} rectangle On rectangle to find an intersection
			 * @param {Rectangle} otherRectangle Another rectangle to find an intersection
			 * @param {Rectangle} [result] The object onto which to store the result.
			 * @returns {Rectangle|undefined} The modified result parameter, a Rectangle instance if none was provided or undefined if there is no intersection.
			 */
			static Rectangle intersection(Rectangle rectangle, Rectangle otherRectangle)
			{
				auto rectangleEast = rectangle.east;
				auto rectangleWest = rectangle.west;

				auto otherRectangleEast = otherRectangle.east;
				auto otherRectangleWest = otherRectangle.west;

				if (rectangleEast < rectangleWest && otherRectangleEast > 0.0)
				{
					rectangleEast += CesiumMath::TWO_PI;
				}
				else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0)
				{
					otherRectangleEast += CesiumMath::TWO_PI;
				}

				if (rectangleEast < rectangleWest && otherRectangleWest < 0.0)
				{
					otherRectangleWest += CesiumMath::TWO_PI;
				}
				else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0)
				{
					rectangleWest += CesiumMath::TWO_PI;
				}

				auto west = CesiumMath::negativePiToPi(
					(std::max)(rectangleWest, otherRectangleWest));
				auto east = CesiumMath::negativePiToPi(
					(std::min)(rectangleEast, otherRectangleEast));

				if (
					(rectangle.west < rectangle.east ||
					 otherRectangle.west < otherRectangle.east) &&
					east <= west)
				{
					return Rectangle();
				}

				auto south = (std::max)(rectangle.south, otherRectangle.south);
				auto north = (std::min)(rectangle.north, otherRectangle.north);

				if (south >= north)
				{
					return Rectangle();
				}

				return Rectangle(west, south, east, north);
			};

			/**
			 * Computes a simple intersection of two rectangles.  Unlike {@link Rectangle.intersection}, this function
			 * does not attempt to put the angular coordinates into a consistent range or to account for crossing the
			 * anti-meridian.  As such, it can be used for rectangles where the coordinates are not simply latitude
			 * and longitude (i.e. projected coordinates).
			 *
			 * @param {Rectangle} rectangle On rectangle to find an intersection
			 * @param {Rectangle} otherRectangle Another rectangle to find an intersection
			 * @param {Rectangle} [result] The object onto which to store the result.
			 * @returns {Rectangle|undefined} The modified result parameter, a Rectangle instance if none was provided or undefined if there is no intersection.
			 */
			static Rectangle simpleIntersection(Rectangle rectangle, Rectangle
																		 otherRectangle)
			{
				auto west = (std::max)(rectangle.west, otherRectangle.west);
				auto south = (std::max)(rectangle.south, otherRectangle.south);
				auto east = (std::min)(rectangle.east, otherRectangle.east);
				auto north = (std::min)(rectangle.north, otherRectangle.north);

				if (south >= north || west >= east)
				{
					return Rectangle();
				}

				return Rectangle(west, south, east, north);
			};

			/**
			 * Computes a rectangle that is the union of two rectangles.
			 *
			 * @param {Rectangle} rectangle A rectangle to enclose in rectangle.
			 * @param {Rectangle} otherRectangle A rectangle to enclose in a rectangle.
			 * @param {Rectangle} [result] The object onto which to store the result.
			 * @returns {Rectangle} The modified result parameter or a Rectangle instance if none was provided.
			 */
			static Rectangle union_(Rectangle rectangle, Rectangle
															 otherRectangle)
			{

				Rectangle result = Rectangle();

				auto rectangleEast = rectangle.east;
				auto rectangleWest = rectangle.west;

				auto otherRectangleEast = otherRectangle.east;
				auto otherRectangleWest = otherRectangle.west;

				if (rectangleEast < rectangleWest && otherRectangleEast > 0.0)
				{
					rectangleEast += CesiumMath::TWO_PI;
				}
				else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0)
				{
					otherRectangleEast += CesiumMath::TWO_PI;
				}

				if (rectangleEast < rectangleWest && otherRectangleWest < 0.0)
				{
					otherRectangleWest += CesiumMath::TWO_PI;
				}
				else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0)
				{
					rectangleWest += CesiumMath::TWO_PI;
				}

				auto west = CesiumMath::negativePiToPi(
					(std::min)(rectangleWest, otherRectangleWest));
				auto east = CesiumMath::negativePiToPi(
					(std::max)(rectangleEast, otherRectangleEast));

				result.west = west;
				result.south = (std::min)(rectangle.south, otherRectangle.south);
				result.east = east;
				result.north = (std::max)(rectangle.north, otherRectangle.north);

				return result;
			};

			/**
			 * Computes a rectangle by enlarging the provided rectangle until it contains the provided cartographic.
			 *
			 * @param {Rectangle} rectangle A rectangle to expand.
			 * @param {Cartographic} cartographic A cartographic to enclose in a rectangle.
			 * @param {Rectangle} [result] The object onto which to store the result.
			 * @returns {Rectangle} The modified result parameter or a Rectangle instance if one was not provided.
			 */
			static Rectangle expand(Rectangle rectangle, Cartographic cartographic)
			{
				Rectangle result = Rectangle();
				result.west = (std::min)(rectangle.west, cartographic.longitude);
				result.south = (std::min)(rectangle.south, cartographic.latitude);
				result.east = (std::max)(rectangle.east, cartographic.longitude);
				result.north = (std::max)(rectangle.north, cartographic.latitude);

				return result;
			};

			/**
			 * Returns true if the cartographic is on or inside the rectangle, false otherwise.
			 *
			 * @param {Rectangle} rectangle The rectangle
			 * @param {Cartographic} cartographic The cartographic to test.
			 * @returns {boolean} true if the provided cartographic is inside the rectangle, false otherwise.
			 */
			static bool contains(Rectangle rectangle, Cartographic cartographic)
			{
				auto longitude = cartographic.longitude;
				auto latitude = cartographic.latitude;

				auto west = rectangle.west;
				auto east = rectangle.east;

				if (east < west)
				{
					east += CesiumMath::TWO_PI;
					if (longitude < 0.0)
					{
						longitude += CesiumMath::TWO_PI;
					}
				}
				return (
					(longitude > west ||
					 CesiumMath::equalsEpsilon(longitude, west, CesiumMath::EPSILON14)) &&
					(longitude < east ||
					 CesiumMath::equalsEpsilon(longitude, east, CesiumMath::EPSILON14)) &&
					latitude >= rectangle.south &&
					latitude <= rectangle.north);
			};

			/**
			 * Samples a rectangle so that it includes a list of Cartesian points suitable for passing to
			 * {@link BoundingSphere#fromPoints}.  Sampling is necessary to account
			 * for rectangles that cover the poles or cross the equator.
			 *
			 * @param {Rectangle} rectangle The rectangle to subsample.
			 * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid to use.
			 * @param {number} [surfaceHeight=0.0] The height of the rectangle above the ellipsoid.
			 * @param {Cartesian3[]} [result] The array of Cartesians onto which to store the result.
			 * @returns {Cartesian3[]} The modified result parameter or a new Array of Cartesians instances if none was provided.
			 */
			std::vector<Cartesian3> subsample(Rectangle rectangle,
											  Ellipsoid ellipsoid = Ellipsoid::WGS84,
											  double surfaceHeight = 0.0)
			{
				std::vector<Cartesian3> result;

				auto length = 0;

				auto north = rectangle.north;
				auto south = rectangle.south;
				auto east = rectangle.east;
				auto west = rectangle.west;

				Cartographic lla;
				lla.height = surfaceHeight;

				lla.longitude = west;
				lla.latitude = north;
				result.push_back(ellipsoid.cartographicToCartesian(lla));
				length++;

				lla.longitude = east;
				result.push_back(ellipsoid.cartographicToCartesian(lla));
				length++;

				lla.latitude = south;
				result.push_back(ellipsoid.cartographicToCartesian(lla));
				length++;

				lla.longitude = west;
				result.push_back(ellipsoid.cartographicToCartesian(lla));
				length++;

				if (north < 0.0)
				{
					lla.latitude = north;
				}
				else if (south > 0.0)
				{
					lla.latitude = south;
				}
				else
				{
					lla.latitude = 0.0;
				}

				for (auto i = 1; i < 8; ++i)
				{
					lla.longitude = -CesiumMath::PI + i * CesiumMath::PI_OVER_TWO;
					if (Rectangle::contains(rectangle, lla))
					{
						result.push_back(ellipsoid.cartographicToCartesian(lla));
						length++;
					}
				}

				if (lla.latitude == 0.0)
				{
					lla.longitude = west;
					result.push_back(ellipsoid.cartographicToCartesian(lla));
					length++;
					lla.longitude = east;
					result.push_back(ellipsoid.cartographicToCartesian(lla));
					length++;
				}

				return result;
			};

			/**
			 * Computes a subsection of a rectangle from normalized coordinates in the range [0.0, 1.0].
			 *
			 * @param {Rectangle} rectangle The rectangle to subsection.
			 * @param {number} westLerp The west interpolation factor in the range [0.0, 1.0]. Must be less than or equal to eastLerp.
			 * @param {number} southLerp The south interpolation factor in the range [0.0, 1.0]. Must be less than or equal to northLerp.
			 * @param {number} eastLerp The east interpolation factor in the range [0.0, 1.0]. Must be greater than or equal to westLerp.
			 * @param {number} northLerp The north interpolation factor in the range [0.0, 1.0]. Must be greater than or equal to southLerp.
			 * @param {Rectangle} [result] The object onto which to store the result.
			 * @returns {Rectangle} The modified result parameter or a Rectangle instance if none was provided.
			 */
			static Rectangle subsection(
				Rectangle rectangle,
				double westLerp,
				double southLerp,
				double eastLerp,
				double northLerp)
			{
				Rectangle result = Rectangle();

				// This function doesn't use CesiumMath::lerp because it has floating point precision problems
				// when the start and end values are the same but the t changes.

				if (rectangle.west <= rectangle.east)
				{
					auto width = rectangle.east - rectangle.west;
					result.west = rectangle.west + westLerp * width;
					result.east = rectangle.west + eastLerp * width;
				}
				else
				{
					auto width = CesiumMath::TWO_PI + rectangle.east - rectangle.west;
					result.west = CesiumMath::negativePiToPi(rectangle.west + westLerp * width);
					result.east = CesiumMath::negativePiToPi(rectangle.west + eastLerp * width);
				}
				auto height = rectangle.north - rectangle.south;
				result.south = rectangle.south + southLerp * height;
				result.north = rectangle.south + northLerp * height;

				// Fix floating point precision problems when t = 1
				if (westLerp == 1.0)
				{
					result.west = rectangle.east;
				}
				if (eastLerp == 1.0)
				{
					result.east = rectangle.east;
				}
				if (southLerp == 1.0)
				{
					result.south = rectangle.north;
				}
				if (northLerp == 1.0)
				{
					result.north = rectangle.north;
				}

				return result;
			};

			/**
			 * Compares this Cartesian against the provided Cartesian componentwise and returns
			 * <code>true</code> if they are equal, <code>false</code> otherwise.
			 *
			 * @param {Rectangle} [right] The right hand side Cartesian.
			 * @returns {boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
			 */
			bool equals(Rectangle right)
			{
				return Rectangle::equals(*this, right);
			};

			/**
			 * Compares the provided Rectangles componentwise and returns
			 * <code>true</code> if they pass an absolute or relative tolerance test,
			 * <code>false</code> otherwise.
			 *
			 * @param {Rectangle} [left] The first Rectangle.
			 * @param {Rectangle} [right] The second Rectangle.
			 * @param {number} [absoluteEpsilon=0] The absolute epsilon tolerance to use for equality testing.
			 * @returns {boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
			 */
			static bool equalsEpsilon(Rectangle left, Rectangle right, double absoluteEpsilon = 0.0)
			{
				return (
					std::abs(left.west - right.west) <= absoluteEpsilon &&
					std::abs(left.south - right.south) <= absoluteEpsilon &&
					std::abs(left.east - right.east) <= absoluteEpsilon &&
					std::abs(left.north - right.north) <= absoluteEpsilon);
			};

			/**
			 * Compares this Cartesian against the provided Cartesian componentwise and returns
			 * <code>true</code> if they pass an absolute or relative tolerance test,
			 * <code>false</code> otherwise.
			 *
			 * @param {Rectangle} [right] The right hand side Cartesian.
			 * @param {number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
			 * @param {number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
			 * @returns {boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
			 */
			bool equalsEpsilon(
				Rectangle &right,
				double absoluteEpsilon = 0.0)
			{
				return Rectangle::equalsEpsilon(
					*this,
					right,
					absoluteEpsilon);
			};

			/**
			 * Creates a string representing this Cartesian in the format '(x, y, z)'.
			 *
			 * @returns {string} A string representing this Cartesian in the format '(x, y, z)'.
			 */
			String toString()
			{
				return str_format("%f,%f,%f,%f",
								  CesiumMath::toDegrees(west),
								  CesiumMath::toDegrees(south),
								  CesiumMath::toDegrees(east),
								  CesiumMath::toDegrees(north));
			};

			const static Rectangle MAX_VALUE;
		};

	}
}

#endif
